import '../public/index'
import url from '../data/url'
import request from '../data/request'
import store from 'store'
import {header} from '../data/config'
import {getUrlParam, fetch} from '../data/util'

import {
  bank as valid_bank,
  checkDeveloperName as valid_checkDeveloperName
} from '../data/validate'

const base = url.base,
  addDeveloper = request.editDeveloperManage,
  getBankList = request.getBankList;

// 页面标题
const title = '卡BIN',
  action = '修改';
let pageTitile = '添加卡BIN'
let id = getUrlParam('id')

const app = {
  data: {},
  created: function () {
    valid()
    if (id) {
      app.getCardBinMsg()
    } else {
      $('.page-title').each((index, element) => {
        $(element).text(pageTitile)
      })
    }
  },
  getCardBinMsg() {
    fetch({url: '/admin/bankBin/modifybankBinInit.json', data: {id: id}}).then(res => {
      app.data = Object.assign({}, res.object)
      app.renderForm()
    })
  },
  renderForm() {
    let elArr = ['bankBin', 'bankCode', 'bankName', 'twoName', 'bankType']
    elArr.forEach(element => {
      $(('#msg-' + element)).val(app.data[element])
    })

  }
}
app.created()

function valid() {

  let form_valid,
    $form = $('#form')

  $.validator.setDefaults({ // 只能放在validate规则配置之前

    // 提交事件
    submitHandler: () => {
      console.log('submit!')

      let loading_modal = layer.msg('提交中...', {
        icon: 16,
        time: 0
      })

      form.isLock = true
      let data = Object.assign({}, $('#form').serializeObject(), id ? {id, id} : null)

      fetch({
        url: '/admin/bankBin/createOrModifyBankBin.json', data: data
      })
        .done(function (res) {
          console.log(res)

          if (res.code === -1) {
            layer.msg(res.message, {
              icon: 5
            })

            return
          }

          let redict = () => location.href = './index.html'
          if (id) {
            layer.msg(`${action}${ title }成功`, {
              icon: 6
            })
            setTimeout(redict(),1000)
          } else {
            layer.msg(`${pageTitile}${ title }成功`, {
              icon: 6
            })
            setTimeout(redict(),1000)
          }

          // 重置表单
          $.extend(form.$data, form.$options.data())

          setTimeout(() => {
            window.location = './index.html'
          }, 1000)
        })
        .fail(function (res) {
          console.log(res)

          layer.msg(`添加${ title }失败`, {
            icon: 5
          })

          console.log("error");
        })
        .always(function (res) {

          layer.close(loading_modal)
          form.isLock = false

        })

    }
  })

  // 验证
  form_valid = $form.validate({
    errorPlacement: function (error, element) {
      error.appendTo(element.parents('.col-xs-24'))
    },
    rules: {
      loginName: {
        required: true
      },
      enterprisePrefix: {
        required: true
      },
      enterpriseName: valid_checkDeveloperName,
      realName: {
        required: true
      },
      phone: {
        phone: true,
        required: true
      },
      merchantBank: {
        // required: true
      },
      bankCardNo: valid_bank

    }
  })

}